(function () {
    var addAreaButtons;

    this.addAreaRow = function (anchor) {
        var anchorRow = anchor.parentNode.parentNode, row, cell, input, num;
        row = this.document.createElement('tr');
        cell = this.document.createElement('th');
        cell.appendChild(this.document.createTextNode(this.document.
                getElementById('areaNameLabel').firstChild.nodeValue));
        row.appendChild(cell);
        cell = this.document.createElement('th');
        input = this.document.createElement('select');
        input.innerHTML = this.document.getElementById('sections').
                getElementsByTagName('select')[0].innerHTML;
        input.name = 'area[]';
        input.onchange = function () {
            var newInput;
            if (input.value === '') {
                newInput = document.createElement('input');
                newInput.name = 'area[]';
                input.parentNode.insertBefore(newInput, input);
                input.parentNode.removeChild(input);
                newInput.focus();
            }
        };
        cell.appendChild(input);
        row.appendChild(cell);
        anchorRow.parentNode.insertBefore(row, anchorRow);
        num = parseInt(input.form.numAreas.value);
        input.form.numAreas.value = num + 1;
        row = this.document.createElement('tr');
        cell = this.document.createElement('td');
        cell.setAttribute('colspan', '2');
        addAreaButtons(cell, num);
        row.appendChild(cell);
        anchorRow.parentNode.insertBefore(row, anchorRow);
    };

    addAreaButtons = function (cell, num) {
        var btn = this.document.createElement('a');
        btn.href = '#';
        btn.onclick = function () {
            addGlobalLabel(this, num);
            return false;
        };
        btn.appendChild(this.document.createTextNode(this.document.
                getElementById('addGlobalLabel').firstChild.nodeValue));
        cell.appendChild(btn);
        cell.appendChild(this.document.createTextNode(' '));
        btn = this.document.createElement('a');
        btn.href = '#';
        btn.onclick = function () {
            addMenu(this, num);
            return false;
        };
        btn.appendChild(this.document.createTextNode(this.document.
                getElementById('addMenuLabel').firstChild.nodeValue));
        cell.appendChild(btn);
        cell.appendChild(this.document.createTextNode(' '));
        btn = this.document.createElement('a');
        btn.href = '#';
        btn.onclick = function () {
            addTemplate(this, num);
            return false;
        };
        btn.appendChild(this.document.createTextNode(this.document.
                getElementById('addTemplateLabel').firstChild.nodeValue));
        cell.appendChild(btn);
        cell.appendChild(this.document.createTextNode(' '));
        btn = this.document.createElement('a');
        btn.href = '#';
        btn.onclick = function () {
            addModule(this, num);
            return false;
        };
        btn.appendChild(this.document.createTextNode(this.document.
                getElementById('addModuleLabel').firstChild.nodeValue));
        cell.appendChild(btn);
    };

    this.addModule = function (anchor, num) {
        var anchorRow = anchor.parentNode.parentNode, row, cell, select, input;
        row = this.document.createElement('tr');
        cell = this.document.createElement('td');
        cell.innerHTML = this.document.getElementById('displayModuleLabel').
                innerHTML;
        row.appendChild(cell);
        cell = this.document.createElement('td');
        input = this.document.createElement('input');
        input.type = 'hidden';
        input.name = 'area' + num + '[]';
        input.value = 'module';
        cell.appendChild(input);
        select = this.document.createElement('select');
        select.name = 'area' + num + 'module[]';
        select.innerHTML = this.document.getElementById('modules').
                getElementsByTagName('select')[0].innerHTML;
        cell.appendChild(select);
        row.appendChild(cell);
        anchorRow.parentNode.insertBefore(row, anchorRow);
        row = this.document.createElement('tr');
        cell = this.document.createElement('td');
        cell.innerHTML = this.document.getElementById('moduleParamsLabel').
                innerHTML;
        row.appendChild(cell);
        cell = this.document.createElement('td');
        input = this.document.createElement('input');
        input.type = 'text';
        input.name = 'area' + num + 'moduleParam[]';
        cell.appendChild(input);
        row.appendChild(cell);
        anchorRow.parentNode.insertBefore(row, anchorRow);
    };

    this.addTemplate = function (anchor, num) {
        var anchorRow = anchor.parentNode.parentNode, row, cell, select, input;
        row = this.document.createElement('tr');
        cell = this.document.createElement('td');
        cell.innerHTML = this.document.getElementById('displayTemplateLabel').
                innerHTML;
        row.appendChild(cell);
        cell = this.document.createElement('td');
        input = this.document.createElement('input');
        input.type = 'hidden';
        input.name = 'area' + num + '[]';
        input.value = 'template';
        cell.appendChild(input);
        select = this.document.createElement('select');
        select.name = 'area' + num + 'template[]';
        select.innerHTML = this.document.getElementById('templates').
                getElementsByTagName('select')[0].innerHTML;
        cell.appendChild(select);
        row.appendChild(cell);
        anchorRow.parentNode.insertBefore(row, anchorRow);
    };

    this.addMenu = function (anchor, num) {
        var anchorRow = anchor.parentNode.parentNode, row, cell, input;
        row = this.document.createElement('tr');
        cell = this.document.createElement('td');
        cell.innerHTML = this.document.getElementById('displayMenuLabel').
                innerHTML;
        row.appendChild(cell);
        cell = this.document.createElement('td');
        input = this.document.createElement('input');
        input.type = 'hidden';
        input.name = 'area' + num + '[]';
        input.value = 'menu';
        cell.appendChild(input);
        input = this.document.createElement('input');
        input.type = 'checkbox';
        input.name = 'area' + num + 'menu[]';
        input.value = '1';
        input.checked = true;
        cell.appendChild(input);
        row.appendChild(cell);
        anchorRow.parentNode.insertBefore(row, anchorRow);
    };

    this.addGlobalLabel = function (anchor, num) {
        var anchorRow = anchor.parentNode.parentNode, row, cell, select, input;
        row = this.document.createElement('tr');
        cell = this.document.createElement('td');
        cell.innerHTML = this.document.getElementById('displayLabelLabel').
                innerHTML;
        row.appendChild(cell);
        cell = this.document.createElement('td');
        input = this.document.createElement('input');
        input.type = 'hidden';
        input.name = 'area' + num + '[]';
        input.value = 'label';
        cell.appendChild(input);
        select = this.document.createElement('select');
        select.name = 'area' + num + 'label[]';
        select.innerHTML = this.document.getElementById('globalLabels').
                getElementsByTagName('select')[0].innerHTML;
        cell.appendChild(select);
        row.appendChild(cell);
        anchorRow.parentNode.insertBefore(row, anchorRow);
    };
})();